﻿Imports System.IO
Imports System.Xml



Public Class FileHandler

    Private _FileError As ErrorHandler
    Public Property FileError() As ErrorHandler
        Get
            Return _FileError
        End Get
        Set(ByVal value As ErrorHandler)
            _FileError = value
        End Set
    End Property

    Private _AllTrades As Generic.List(Of Trade)
    Public Property AllTrades() As Generic.List(Of Trade)
        Get
            Return _AllTrades
        End Get
        Set(ByVal value As Generic.List(Of Trade))
            _AllTrades = value
        End Set
    End Property



#Region "Public Methods"
    Function ReadInFiles() As Generic.List(Of Trade)

        Me.FileError = New ErrorHandler
        Me.AllTrades = New Generic.List(Of Trade)
        Try
            Dim sFiles() As String = Directory.GetFiles(My.Settings.sInputDir, "*" & My.Settings.sFileMask & "*")

            If sFiles.Count = 0 Then
                Logger.MsgLog("No files found in selected dir (" & My.Settings.sInputDir & ")")
                Return Me.AllTrades
                Exit Function
            End If

            If My.Settings.bFileUploadOnly Then
                'TODO upload only code
            Else
                For Each sFile In sFiles
                    Select Case My.Settings.sInputFileType
                        Case "FXC V6 XML"
                            Me.InputV6XML(sFile)
                        Case "FXC V6 CSV"
                            Me.InputV6CSV(sFile)
                        Case "Settlement Center XML"
                            Me.InputSettlementCenterXML(sFile)
                        Case "Currenex XML"
                            Me.InputCEXML(sFile)
                        Case "Swift File"
                            Me.InputSwiftFile(sFile)
                        Case "CRD"
                            'TODO CRD Function
                        Case "Custom File"
                            Me.InputCustom(sFile)
                        Case Else
                            Throw New InvalidOperationException("No input file type selected.")
                    End Select
                Next
            End If
            Return Me.AllTrades
        Catch ex As Exception
            Logger.ErrorLog(ex, "File Handler")
            End
        End Try
    End Function




#End Region

#Region "Private Methods"

    Private Sub InputV6CSV(ByVal FilePath As String)

        Dim sLine As String
        Dim asLine() As String
        Dim sBroker, sTradeDate, sDateTime As String
        sBroker = String.Empty
        sTradeDate = String.Empty
        sDateTime = String.Empty

        Using srReader As New StreamReader(FilePath)

            Do While srReader.Peek <> -1
                sLine = srReader.ReadLine
                asLine = Split(sLine, ",")

                Select Case asLine.Length
                    Case 13, 10 ' 13 for 6.4 file, 10 for 6.3 file
                        If asLine(0) <> "Session Type" Then ' only grabs information if it is the data row, not the header
                            sBroker = asLine(1) 'Broker
                            sTradeDate = Left(asLine(8), 8)
                            sDateTime = asLine(8)
                        End If
                    Case Is > 13
                        If asLine(myConstants.V6CSV.TradeStatus) = "FILLED" Then 'AndAlso asLine(myConstants.V6CSV.TradeAction) = "ACCEPTED" Then
                            Dim aTrade As New Trade
                            aTrade.OrderType = asLine(myConstants.V6CSV.OrderType)
                            aTrade.QuotePair = asLine(myConstants.V6CSV.CCYPair)
                            aTrade.Side = asLine(myConstants.V6CSV.Side)
                            aTrade.GivenCCY = asLine(myConstants.V6CSV.GivenCCY)
                            aTrade.GivenAmt = asLine(myConstants.V6CSV.GivenAmt)
                            aTrade.CounterCCY = asLine(myConstants.V6CSV.CounterCCY)
                            If Date.TryParse(asLine(myConstants.V6CSV.ValDate), aTrade.ValueDate) = False Then
                                'code to parse date
                                aTrade.ValueDate = Trade.ConvertDate(asLine(myConstants.V6CSV.ValDate), "yyyyMMdd")
                            End If
                            If Date.TryParse(sTradeDate, aTrade.TradeDate) = False Then
                                'code to parse date
                                aTrade.TradeDate = Trade.ConvertDate(sTradeDate, "yyyyMMdd")
                            End If
                            If My.Settings.bTradeIDInFile Then aTrade.ClientID = My.Settings.sTradeIDPrefix.Trim & asLine(myConstants.V6CSV.ClientTradeID)
                            aTrade.Comments = asLine(myConstants.V6CSV.Comments)
                            aTrade.Fund = My.Settings.AllFunds.PopulateFund(asLine(myConstants.V6CSV.Fund))
                            aTrade.SptFwdFlag = asLine(myConstants.V6CSV.SpotFwd)
                            aTrade.OCFlag = asLine(myConstants.V6CSV.OpenClose)
                            aTrade.Link = asLine(myConstants.V6CSV.Fund)
                            aTrade.LimitType = asLine(myConstants.V6CSV.LimitType)
                            aTrade.ReqRate = asLine(myConstants.V6CSV.ReqRate)
                            aTrade.ExpType = asLine(myConstants.V6CSV.ExpType)
                            aTrade.Zone = asLine(myConstants.V6CSV.Zone)
                            aTrade.ExpDate = asLine(myConstants.V6CSV.ExpDate)
                            aTrade.ExpTime = asLine(myConstants.V6CSV.ExpTime)
                            aTrade.FillAll = asLine(myConstants.V6CSV.FillAll)
                            aTrade.CounterAmt = asLine(myConstants.V6CSV.CounterAmt)
                            aTrade.SpotRate = asLine(myConstants.V6CSV.SpotRate)
                            aTrade.FwdPts = asLine(myConstants.V6CSV.FwdPts)
                            Double.TryParse(asLine(myConstants.V6CSV.SwapPts), aTrade.SwapPts)
                            aTrade.AllInRate = asLine(myConstants.V6CSV.AllInRate)
                            If Date.TryParse(asLine(myConstants.V6CSV.FXCFixingDate), aTrade.FixingDate) = False AndAlso _
                            Not asLine(myConstants.V6CSV.FXCFixingDate) = String.Empty Then
                                aTrade.FixingDate = Trade.ConvertDate(asLine(myConstants.V6CSV.FXCFixingDate), "yyyyMMdd")
                            End If
                            aTrade.AmtFilled = asLine(myConstants.V6CSV.AmtFilled)
                            aTrade.EffRate = asLine(myConstants.V6CSV.EffRate)
                            aTrade.FXCOrderID = asLine(myConstants.V6CSV.FXCID)
                            aTrade.BlockID = asLine(myConstants.V6CSV.BlockID)
                            aTrade.ClientBlockID = asLine(myConstants.V6CSV.ClientBlockID)
                            aTrade.Status = asLine(myConstants.V6CSV.TradeStatus)
                            aTrade.Action = asLine(myConstants.V6CSV.TradeAction)
                            aTrade.Data1 = asLine(myConstants.V6CSV.Data1)
                            aTrade.Data2 = asLine(myConstants.V6CSV.Data2)
                            aTrade.Data3 = asLine(myConstants.V6CSV.Data3)
                            aTrade.Data4 = asLine(myConstants.V6CSV.Data4)
                            aTrade.Data5 = asLine(myConstants.V6CSV.Data5)
                            aTrade.Custom1 = asLine(myConstants.V6CSV.Custom1)
                            aTrade.Custom2 = asLine(myConstants.V6CSV.Custom2)
                            aTrade.Custom3 = asLine(myConstants.V6CSV.Custom3)
                            aTrade.Custom4 = asLine(myConstants.V6CSV.Custom4)
                            aTrade.Custom5 = asLine(myConstants.V6CSV.Custom5)
                            aTrade.Operation = "NEWT"
                            aTrade.DateTime = sDateTime
                            aTrade.Broker = My.Settings.AllBrokers.PopulateBroker(sBroker)

                            Me.AllTrades.Add(aTrade)

                        End If
                End Select

            Loop
        End Using
    End Sub
    
    Private Sub InputCustom(ByVal FilePath As String)
        Dim sLine As String
        Dim sLineArr() As String
        Dim sDelimiter As String
        Dim iRowCount As Integer = 1 'starting row count at 1, rather than 0
        Dim iRowTotal As Integer
        Select Case My.Settings.sCustomFileDelim.ToLower
            Case "comma"
                sDelimiter = ","
            Case "space"
                sDelimiter = " "
            Case "tab"
                sDelimiter = vbTab
            Case "semicolon"
                sDelimiter = ";"
            Case "pipe"
                sDelimiter = "|"
            Case Else
                sDelimiter = ","
        End Select

        Using srReader As New StreamReader(FilePath)
            iRowTotal = srReader.ReadToEnd.Split(vbCrLf).Count
            Do While srReader.Peek <> -1

                sLine = srReader.ReadLine

                If sLine = "" OrElse iRowCount < My.Settings.iHeaderRows OrElse iRowCount > (iRowTotal - My.Settings.iFooterRows) Then Continue Do

                sLineArr = Split(sLine, sDelimiter)

                Dim aTrade As New Trade
                '***mandatory fields 
                If My.Settings.bTradeIDInFile Then aTrade.ClientID = My.Settings.sTradeIDPrefix.Trim & sLineArr(My.Settings.iColTradeID - 1)
                aTrade.GivenCCY = sLineArr(My.Settings.iColCCY1 - 1).Trim
                aTrade.CounterCCY = sLineArr(My.Settings.iColCCy2 - 1).Trim
                aTrade.GivenAmt = sLineArr(My.Settings.iColAmt1 - 1).Trim
                aTrade.CounterAmt = sLineArr(My.Settings.iColAmt2 - 1).Trim
                aTrade.AllInRate = sLineArr(My.Settings.iColAllIn - 1).Trim
                aTrade.Fund = My.Settings.AllFunds.PopulateFund(My.Settings.iColFund - 1)
                aTrade.Broker = My.Settings.AllBrokers.PopulateBroker(sLineArr(My.Settings.iColAmt1 - 1).Trim)
                If Date.TryParse(sLineArr(My.Settings.iColVD - 1).Trim, aTrade.ValueDate) = False Then
                    'code to parse date
                    aTrade.ValueDate = Trade.ConvertDate(sLineArr(My.Settings.iColVD - 1).Trim, My.Settings.sCustDateFormat)
                End If
                If Date.TryParse(sLineArr(My.Settings.iColTD - 1).Trim, aTrade.TradeDate) = False Then
                    'code to parse date
                    aTrade.TradeDate = Trade.ConvertDate(sLineArr(My.Settings.iColTD - 1).Trim, My.Settings.sCustDateFormat)
                End If
                'optional data
                aTrade.Side = sLineArr(My.Settings.iColBuyFlag - 1)
                aTrade.SptFwdFlag = sLineArr(My.Settings.iColSpotFwdFlag - 1)
                aTrade.FixingDate = sLineArr(My.Settings.iColFixingDate - 1)
                aTrade.NetFlag = sLineArr(My.Settings.iColNettingFlag - 1)
                aTrade.Status = sLineArr(My.Settings.iColTradeStatus - 1)
                aTrade.CLSBypassFlag = sLineArr(My.Settings.iColBypassCLSFlag - 1)


                Select Case Left(aTrade.Status.ToLower.Trim, 1)
                    Case "n"
                        aTrade.Operation = "NEWT"
                    Case "a", "m"
                        aTrade.Operation = "AMND"
                    Case "c"
                        aTrade.Operation = "CANC"
                    Case Else
                        aTrade.Operation = "NEWT"

                End Select


                'check for mandatory fields
                If aTrade.GivenCCY = "" OrElse aTrade.CounterCCY = "" OrElse aTrade.GivenAmt = "" OrElse aTrade.CounterAmt = "" OrElse _
                aTrade.AllInRate = "" OrElse aTrade.ValueDate = "" OrElse aTrade.TradeDate = "" Then
                    Throw New InvalidOperationException("A mandatory data field is not returning a value.  Please check your input file configuration.")
                End If

                aTrade.NormalizeSptFwd()
                aTrade.NormalizeSide()

                Me.AllTrades.Add(aTrade)

                iRowCount += 1
            Loop

        End Using

    End Sub

    Private Sub InputV6XML(ByVal sFilePath As String)
        'no support for limit orders.  
        Dim xmlFile As New Xml.XmlDocument
        xmlFile.Load(sFilePath)
        Dim xmlParentNodes As XmlNodeList = xmlFile.ChildNodes(0).ChildNodes
        Dim nlTradeType As XmlNodeList = xmlParentNodes(1).ChildNodes
        Dim nlTrades As XmlNodeList = xmlParentNodes(1).ChildNodes
        Dim sVal As String
        Dim brkCurrent As Broker


        'specialized handling for each trade type
        'Select Case xmlParentNodes(1).Name

        '    Case "marketTrade"

        '    Case "cPTrade"

        'End Select

        For Each xnSessionDetail As XmlNode In nlTrades
            sVal = xnSessionDetail.InnerText
            Select Case xnSessionDetail.Name.Split(":")(1)
                'Case "tradeAssignment"
                Case "counterpartyInfo"
                    'get cp info from nodelist
                    Dim nlCP As XmlNodeList = xnSessionDetail.ChildNodes
                    brkCurrent = My.Settings.AllBrokers.PopulateBroker(nlCP(1).InnerXml)
                Case "orderList"
                    'get order info from nodelist
                    Dim nlOrders As XmlNodeList = xnSessionDetail.ChildNodes
                    For Each xnOrder As XmlNode In nlOrders
                        If xnOrder.NodeType = XmlNodeType.Comment Then Continue For
                        Dim aTrade As New Trade
                        Dim bIsNDF As Boolean = False
                        'popuplate from attributes in swap/outright/ndf node
                        aTrade.Side = xnOrder.Attributes("com:side").Value
                        aTrade.Operation = "NEWT"
                        aTrade.Broker = brkCurrent
                        For Each xnDeet As XmlNode In xnOrder.ChildNodes
                            'step through order
                            Select Case xnDeet.Name.Split(":")(1)
                                Case "clientIdentifier"
                                    aTrade.Fund = My.Settings.AllFunds.PopulateFund(xnDeet.Attributes("com:account").Value)
                                    aTrade.ClientID = xnDeet.Attributes("com:clientId").Value
                                Case "privateComment"
                                    aTrade.Comments = xnDeet.InnerXml
                                Case "currencyPair"
                                    aTrade.GivenCCY = xnDeet.Attributes("com:givenCCY").Value
                                    aTrade.CounterCCY = xnDeet.Attributes("com:settleCCY").Value
                                    aTrade.QuotePair = xnDeet.Attributes("com:quotePair").Value
                                Case "givenAmount"
                                    aTrade.GivenAmt = xnDeet.InnerXml
                                Case "OC"
                                    aTrade.OCFlag = xnDeet.InnerXml
                                Case "Data_1"
                                    aTrade.Data1 = xnDeet.InnerXml
                                Case "Data_2"
                                    aTrade.Data2 = xnDeet.InnerXml
                                Case "Data_3"
                                    aTrade.Data3 = xnDeet.InnerXml
                                Case "Data_4"
                                    aTrade.Data4 = xnDeet.InnerXml
                                Case "Custom_5"
                                    aTrade.Custom5 = xnDeet.InnerXml
                                Case "Custom_1"
                                    aTrade.Custom1 = xnDeet.InnerXml
                                Case "Custom_2"
                                    aTrade.Custom2 = xnDeet.InnerXml
                                Case "Custom_3"
                                    aTrade.Custom3 = xnDeet.InnerXml
                                Case "Custom_4"
                                    aTrade.Custom4 = xnDeet.InnerXml
                                Case "Custom_5"
                                    aTrade.Custom5 = xnDeet.InnerXml
                                Case "tradeDate"
                                    aTrade.TradeDate = Trade.ConvertDate(xnDeet.InnerXml, "yyyy-MM-dd")
                                Case "valueDate"
                                    aTrade.ValueDate = Trade.ConvertDate(xnDeet.InnerXml, "yyyy-MM-dd")
                                Case "fixingDate"
                                    aTrade.FixingDate = Trade.ConvertDate(xnDeet.InnerXml, "yyyy-MM-dd")
                                Case "isNDF"
                                    bIsNDF = xnDeet.InnerXml
                                Case "indicator"
                                    aTrade.SptFwdFlag = xnDeet.InnerXml
                                Case "rate"
                                    aTrade.SpotRate = xnDeet.Attributes("com:spot").Value
                                    aTrade.FwdPts = xnDeet.Attributes("com:fwdPoints").Value
                                Case "swapPts"
                                    aTrade.SwapPts = xnDeet.InnerXml
                                Case "allInRate"
                                    aTrade.AllInRate = xnDeet.InnerXml
                                Case "settleAmount"
                                    aTrade.CounterAmt = xnDeet.InnerXml
                                Case "status"
                                    aTrade.Status = xnDeet.InnerXml
                                Case "action"
                                    aTrade.Action = xnDeet.InnerXml
                                Case "clientBlockId"
                                    aTrade.ClientBlockID = xnDeet.InnerXml
                                Case "indicativeRateInfo"
                                    Dim dblMinRate As Double = xnDeet.ChildNodes(0).InnerXml
                                    Dim dblMaxRate As Double = xnDeet.ChildNodes(1).InnerXml
                                    Dim sTimeStamp As String = xnDeet.ChildNodes(2).InnerXml
                                    Dim sDirection As String = xnDeet.ChildNodes(3).InnerXml
                                    'code here for handling Market Sessions with Indicative Rates

                            End Select
                        Next
                        If aTrade.Status.ToLower.Trim = "filled" Then Me.AllTrades.Add(aTrade)
                    Next


            End Select

        Next

    End Sub

    Private Sub InputCEXML(ByVal sFilePath As String)

        Dim xmlDocument As New XmlDocument
        Dim sTradeType As String = String.Empty
        Dim bIsSwap As Boolean

        xmlDocument.Load(sFilePath)


        Dim aTradeNodeList As XmlNodeList = XmlDocument.GetElementsByTagName("trade")
        Dim aTradeNode As XmlNode

        For Each aTradeNode In aTradeNodeList
            Dim aTrade As New Trade
            bIsSwap = False
            aTrade.Status = aTradeNode.SelectSingleNode("tradeHeader/tradeStatus").InnerText

            aTrade.GivenCCY = aTradeNode.SelectSingleNode("tradeRequest/specifiedMoney/currency").InnerText
            aTrade.CounterCCY = aTradeNode.SelectSingleNode("tradeRequest/againstCurrency").InnerText

            If aTrade.GivenCCY = aTradeNode.SelectSingleNode("product/fxLeg/cashFlow1/currency").InnerText Then
                aTrade.GivenAmt = aTradeNode.SelectSingleNode("product/fxLeg/cashFlow1/amount").InnerText
                aTrade.CounterAmt = aTradeNode.SelectSingleNode("product/fxLeg/cashFlow2/amount").InnerText
            ElseIf aTrade.GivenCCY = aTradeNode.SelectSingleNode("product/fxLeg/cashFlow2/currency").InnerText Then
                aTrade.CounterAmt = aTradeNode.SelectSingleNode("product/fxLeg/cashFlow1/amount").InnerText
                aTrade.GivenAmt = aTradeNode.SelectSingleNode("product/fxLeg/cashFlow2/amount").InnerText
            Else
            End If

            aTrade.ValueDate = Trade.ConvertDate(aTradeNode.SelectSingleNode("product/fxLeg/valueDate").InnerText, "yyyy-MM-dd")
            Dim nTradeID As XmlNode = aTradeNode.SelectSingleNode("tradeHeader/partyTradeIdentifier").NextSibling
            aTrade.ClientID = nTradeID.SelectSingleNode("tradeId").InnerText & "A"
            aTrade.Side = aTradeNode.SelectSingleNode("tradeRequest/buySell").InnerText
            aTrade.Operation = aTradeNode.SelectSingleNode("tradeHeader/event/eventType").InnerText
            aTrade.AllInRate = aTradeNode.SelectSingleNode("product/fxLeg/exchangeRate/rate").InnerText

            aTrade.TradeDate = Trade.ConvertDate(aTradeNode.SelectSingleNode("tradeHeader/tradeDate").InnerText, "yyyy-MM-dd")
            aTrade.Fund = My.Settings.AllFunds.PopulateFund(aTradeNode.SelectSingleNode("tradeHeader/subFund").InnerText)


            Dim ssellsidetempNode As String
            Dim sbuysidetempNode As String
            Dim ssellsidetempAtt As String
            Dim sbuysidetempAtt As String
            Dim aSellSideAtts As XmlAttributeCollection
            Dim aBuySideAtts As XmlAttributeCollection
            Dim nSellSideBroker As XmlNode = aTradeNode.SelectSingleNode("tradeHeader/partyRole")
            Dim nBuySideBroker As XmlNode = nSellSideBroker.NextSibling

            sbuysidetempNode = nBuySideBroker.InnerText
            ssellsidetempNode = nSellSideBroker.InnerText

            nSellSideBroker = nSellSideBroker.FirstChild
            nBuySideBroker = nBuySideBroker.FirstChild

            aSellSideAtts = nSellSideBroker.Attributes
            aBuySideAtts = nBuySideBroker.Attributes
            ssellsidetempAtt = aSellSideAtts(0).Value
            sbuysidetempAtt = aBuySideAtts(0).Value

            If ssellsidetempNode = "Maker" Then
                ssellsidetempAtt = aSellSideAtts(0).Value
                sbuysidetempAtt = aBuySideAtts(0).Value
            ElseIf sbuysidetempNode = "Maker" Then
                ssellsidetempAtt = aBuySideAtts(0).Value
                sbuysidetempAtt = aSellSideAtts(0).Value
            Else
            End If

            ssellsidetempAtt = Left(ssellsidetempAtt, ssellsidetempAtt.Length - 2)
            sbuysidetempAtt = Left(sbuysidetempAtt, sbuysidetempAtt.Length - 2)

            aTrade.Broker = My.Settings.AllBrokers.PopulateBroker(ssellsidetempAtt)

            If aTradeNode.SelectSingleNode("product/productType").InnerText = "OR" Then
                sTradeType = "FWRD"
                aTrade.SptFwdFlag = "FWD"
                bIsSwap = False
            ElseIf aTradeNode.SelectSingleNode("product/productType").InnerText = "SP" Then
                sTradeType = "SPOT"
                aTrade.SptFwdFlag = "SPOT"
                bIsSwap = False
            ElseIf aTradeNode.SelectSingleNode("product/productType").InnerText = "SW" Then
                sTradeType = "SWAP"
                bIsSwap = True
                Dim sBuySellSwap As String
                sBuySellSwap = aTrade.Side
                Dim aSwapTrade As New Trade

                If sBuySellSwap = "BUY" Then aSwapTrade.Side = "SELL"
                If sBuySellSwap = "SELL" Then aSwapTrade.Side = "BUY"

                Dim aSwapTradeNode As XmlNode = aTradeNode.SelectSingleNode("product/fxLeg").NextSibling
                aSwapTrade.ValueDate = aSwapTradeNode.SelectSingleNode("valueDate").InnerText
                aSwapTrade.ValueDate = Replace(aSwapTrade.ValueDate, "-", "")
                aSwapTrade.ClientID = nTradeID.InnerText & "B"
                aSwapTrade.AllInRate = aSwapTradeNode.SelectSingleNode("exchangeRate/rate").InnerText
                aSwapTrade.TradeDate = aTrade.TradeDate
                aSwapTrade.Status = aTrade.Status
                aSwapTrade.GivenCCY = aTrade.CounterCCY
                aSwapTrade.CounterCCY = aTrade.GivenCCY
                aSwapTrade.GivenAmt = aTrade.CounterAmt
                aSwapTrade.CounterAmt = aTrade.GivenAmt
                aSwapTrade.ClientID = nTradeID.SelectSingleNode("tradeId").InnerText & "B"
                aSwapTrade.OrderType = sTradeType
                aSwapTrade.Operation = aTrade.Operation

                If aSwapTrade.Status = "DONE" Then
                    Me.AllTrades.Add(aSwapTrade)
                Else
                End If


            Else
            End If

            aTrade.OrderType = sTradeType
            If aTrade.Status = "DONE" Then
                Me.AllTrades.Add(aTrade)
            Else
            End If
        Next


    End Sub

    Private Sub InputSwiftFile(ByRef sInputFile As String)
        Dim sCheckArr() As String = {"PUTNU", ":15A:", "BRANU", "15A"}
        Dim sLine As String
        Dim bPutnam As Boolean
        Dim iRowCount As Integer
        Dim sField21 As String = String.Empty


        Using srReader As New StreamReader(sInputFile)

            Do While srReader.Peek <> -1

                sLine = srReader.ReadLine
                sLine = Trim(sLine)

                If Not sCheckArr.Contains(Left(sLine, 5)) Then
                    irowcount = 0
                    sField21 = ""
                    bPutnam = True
                End If

                'If Left(sLine, 5) <> "PUTNU" AndAlso Left(sLine, 5) <> ":15A:" AndAlso Left(sLine, 5) <> "BRANU" AndAlso Left(sLine, 5) <> "15A" Then GoTo skipdim
                'irowcount = 0
                'sField21 = ""
                'bPutnam = True

                Dim aTrade As New Trade

                iRowCount += 1
                'If InStr(sLine, "8HG8HH5524042G") Then
                '    bPutnam = True
                'End If
                Dim s As String = Left(sLine, 5)
                Select Case Left(sLine, 5)


                    Case "PUTNU"
                        bPutnam = True
                    Case "BRANU"
                        bPutnam = True
                    Case ":94A:"
                        sLine = sLine.Remove(0, 5)
                        If sLine = "ASET" Then
                            aTrade.SptFwdFlag = "SPOT"
                        ElseIf sLine = "AFWD" Then
                            aTrade.SptFwdFlag = "FWD"
                        Else
                            aTrade.SptFwdFlag = ""
                        End If

                    Case ":22:N"
                        sLine = sLine.Remove(0, 8)
                        aTrade.CommonRef = sLine

                    Case ":22:C"
                        If Left(sLine, 10) = ":22:CANCEL" Then
                            aTrade.Operation = "CANC"
                            If bPutnam = True Then
                                aTrade.CommonRef = sLine.Remove(0, 11)
                                aTrade.ClientID = sField21
                            End If
                        End If

                    Case ":72:/"
                        If Left(sLine, 12) = ":72:/CORPID=" Then
                            sLine = sLine.Remove(0, 12)
                            If Right(sLine, 1) = "/" Then sLine = sLine.Replace("/", "")
                            aTrade.Fund = My.Settings.AllFunds.PopulateFund(sLine)
                        End If

                    Case ":32R:"
                        Dim stempstring As String = String.Empty
                        sLine = sLine.Replace(Left(sLine, 5), "")
                        aTrade.ValueDate = Trade.ConvertDate("20" & Left(sLine, 6), "yyyyMMdd")
                        stempstring = sLine.Replace(Left(sLine, 6), "")
                        aTrade.GivenCCY = Left(stempstring, 3)
                        stempstring = stempstring.Replace(Left(stempstring, 3), "")
                        aTrade.GivenAmt = stempstring
                    Case ":33P:"
                        Dim stempstring As String = String.Empty
                        stempstring = Replace(Trim(sLine), Left(sLine, 11), "")
                        aTrade.CounterCCY = Left(stempstring, 3)
                        stempstring = stempstring.Replace(Left(stempstring, 3), "")
                        aTrade.CounterAmt = stempstring
                    Case ":22A:"
                        aTrade.Operation = sLine.Remove(0, 5)
                    Case ":82D:"
                        sLine = sLine.Remove(0, 5)
                        ' aTrade.BICCode = sLine.Replace("/ABIC/", "")
                    Case ":87A:"
                        sLine = sLine.Replace("/ABIC/", "")
                        sLine = sLine.Remove(0, 5)
                        Dim sTempBIC As String = sLine
                        If InStr(sLine, "/") > 0 Then
                            sLine = Left(sTempBIC, 9)
                            If Right(sLine, 1) = "/" Then
                                sLine = Left(sLine, 8)
                            Else
                                sLine = Left(sTempBIC, 9)
                                If Right(sLine, 1) = "/" Then
                                    sLine = Left(sLine, 11)
                                End If
                            End If

                        End If
                        aTrade.Broker = My.Settings.AllBrokers.PopulateBroker(sLine)
                    Case ":87J:"
                        sLine = sLine.Replace("/ABIC/", "")
                        sLine = sLine.Remove(0, 5)
                        Dim sTempBIC As String = sLine
                        sTempBIC = sLine
                        If InStr(sLine, "/") > 0 Then
                            sLine = Left(sTempBIC, 9)
                            If Right(sLine, 1) = "/" Then
                                sLine = Left(sLine, 8)
                            Else
                                sLine = Left(sTempBIC, 9)
                                If Right(sLine, 1) = "/" Then
                                    sLine = Left(sLine, 11)
                                End If
                            End If
                            aTrade.Broker = My.Settings.AllBrokers.PopulateBroker(sLine)
                        End If
                    Case ":83J:"
                        sLine = sLine.Remove(0, 11)
                        aTrade.Fund = My.Settings.AllFunds.PopulateFund(sLine)
                    Case ":30T:"
                        sLine = sLine.Remove(0, 5)
                        aTrade.TradeDate = Trade.ConvertDate(sLine, "yyyyMMdd")
                    Case ":30V:"
                        sLine = sLine.Remove(0, 5)
                        aTrade.ValueDate = Trade.ConvertDate(sLine, "yyyyMMdd")

                    Case ":32B:"
                        sLine = sLine.Remove(0, 5)
                        aTrade.GivenCCY = Left(sLine, 3)
                        sLine = sLine.Remove(0, 3)
                        aTrade.GivenAmt = sLine
                    Case ":33B:"
                        sLine = sLine.Remove(0, 5)
                        aTrade.CounterCCY = Left(sLine, 3)
                        sLine = sLine.Remove(0, 3)
                        aTrade.CounterAmt = sLine
                    Case "-"
                        Me.AllTrades.Add(aTrade)
                    Case Else
                        If Left(sLine, 4) = ":36:" Then
                            sLine = sLine.Remove(0, 4)
                            aTrade.AllInRate = sLine
                        ElseIf Left(sLine, 2) = "-}" Then
                            Me.AllTrades.Add(aTrade)
                        ElseIf Left(sLine, 4) = ":30:" Then
                            sLine = sLine.Remove(0, 4)
                            aTrade.TradeDate = Trade.ConvertDate("20" & sLine, "yyyyMMdd")
                        ElseIf iRowCount = 3 Then
                            If bPutnam = True Then
                                aTrade.Broker = My.Settings.AllBrokers.PopulateBroker(sLine)
                            End If
                        ElseIf Left(sLine, 4) = ":20:" Then
                            aTrade.ClientID = sLine.Remove(0, 4)
                        ElseIf Left(sLine, 4) = ":21:" Then
                            sLine = sLine.Remove(0, 4)
                            sField21 = sLine
                            If Left(sLine, 3) = "NEW" Then aTrade.Operation = "NEWT"
                            If Left(sLine, 3) = "CAN" Then aTrade.Operation = "CANC"
                            If Left(sLine, 3) = "AMN" Then aTrade.Operation = "AMND"
                        End If
                End Select
            Loop
        End Using
    End Sub

    Private Sub InputSettlementCenterXML(ByVal FilePath As String)

        Dim xmlDocument As New XmlDocument
        Dim g_V6_XML As New System.Xml.XmlDocument
        Dim sBroker, sTradeDate, sDateTime As String

        sBroker = String.Empty
        sTradeDate = String.Empty
        sDateTime = String.Empty

        Using srReader As New StreamReader(FilePath)

            xmlDocument.Load(FilePath)
            Dim sXMLDocName = xmlDocument.LastChild.Name
            Dim sSessionType = xmlDocument.LastChild.Name
            Dim sHeaderInfo = xmlDocument.FirstChild.Name
            Dim aTradeNodeList As XmlNodeList = xmlDocument.GetElementsByTagName("settlement")
            Dim aTradeNode As XmlNode
            Dim SomeTrades As XmlNodeList
            SomeTrades = xmlDocument.SelectSingleNode("/settlementCenter/settlement").ChildNodes

            For Each aTradeNode In aTradeNodeList
                Dim aTrade As New Trade
                aTrade.Broker = My.Settings.AllBrokers.PopulateBroker(aTradeNode.SelectSingleNode("counterParty").InnerText)
                aTrade.ClientID = aTradeNode.SelectSingleNode("referenceID").InnerText
                aTrade.Operation = aTradeNode.SelectSingleNode("operation").InnerText
                aTrade.Fund = My.Settings.AllFunds.PopulateFund(aTradeNode.SelectSingleNode("accountId").InnerText)
                aTrade.TradeDate = Trade.ConvertDate(aTradeNode.SelectSingleNode("tradeDate").InnerText, "yyyyMMdd")
                aTrade.ValueDate = Trade.ConvertDate(aTradeNode.SelectSingleNode("valueDate").InnerText, "yyyyMMdd")
                aTrade.CounterCCY = aTradeNode.SelectSingleNode("soldCurrency").InnerText
                aTrade.CounterAmt = aTradeNode.SelectSingleNode("soldAmount").InnerText
                aTrade.AllInRate = aTradeNode.SelectSingleNode("rate").InnerText
                aTrade.GivenCCY = aTradeNode.SelectSingleNode("boughtCurrency").InnerText
                aTrade.GivenAmt = aTradeNode.SelectSingleNode("boughtAmount").InnerText
                Try
                    aTrade.NetFlag = aTradeNode.SelectSingleNode("flagNetting").InnerText
                Catch ex As Exception
                    aTrade.NetFlag = ""
                End Try
                Try
                    aTrade.FixingDate = aTradeNode.SelectSingleNode("fixingDate").InnerText
                Catch ex As Exception
                    aTrade.FixingDate = ""
                End Try
                Try
                    aTrade.OCFlag = aTradeNode.SelectSingleNode("flagClose").InnerText
                    If UCase(Left(aTrade.OCFlag, 1)) = "Y" Then
                        aTrade.OCFlag = "/FIX/CLOSENDF"
                        aTrade.FixingDate = ""
                    Else
                        aTrade.OCFlag = ""
                    End If
                Catch ex As Exception
                    aTrade.OCFlag = ""
                End Try
                Try
                    aTrade.SptFwdFlag = Left(aTradeNode.SelectSingleNode("flagType").InnerText.ToUpper, 1)
                    If aTrade.SptFwdFlag = "F" Or aTrade.SptFwdFlag = "N" Then
                        aTrade.SptFwdFlag = "|/FWD/"
                        'aTrade.Account = aTrade.Account & "|/FWD/"
                    ElseIf aTrade.SptFwdFlag = "S" Then
                        'aTrade.Account = aTrade.Account & "|/SPOT/"
                        aTrade.SptFwdFlag = "|/SPOT/"
                    Else
                    End If

                Catch ex As Exception
                    aTrade.SptFwdFlag = ""
                End Try

                Me.AllTrades.Add(aTrade)
            Next
        End Using
    End Sub

#End Region


End Class
